package com.cjx.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.cjx.api.common.*;
import com.cjx.api.exception.BusinessException;
import com.cjx.api.model.dto.productinfo.ProductInfoAddRequest;
import com.cjx.api.model.dto.productinfo.ProductInfoQueryRequest;
import com.cjx.api.model.dto.productinfo.ProductInfoUpdateRequest;
import com.cjx.api.model.entity.ProductInfo;
import com.cjx.api.model.entity.User;
import com.cjx.api.model.vo.ProductInfoVO;
import com.cjx.api.service.ProductInfoService;
import com.cjx.api.service.UserService;
import com.cjx.starsapisdk.client.StarsApiClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

import static com.cjx.api.constant.UserConstant.DEFAULT_ROLE;

/**
 * 产品信息产品
 *
 * @author cjx
 */
@Tag(name = "产品信息产品", description = "产品接口调用相关操作")
@RestController
@RequestMapping("/productInfo")
public class ProductInfoController {

    @Resource
    private ProductInfoService productInfoService;
    @Resource
    private  StarsApiClient starsApiClient;
    @Resource
    private UserService userService;
    // region 增删改查



    /**
     * 新增产品信息
     *
     * @param productInfoAddRequest
     * @param request
     * @return
     */
    @Operation(summary = "新增产品信息", description = "新增产品信息")
    @PostMapping("/add")
    public BaseResponse<Boolean> addProductInfo(@RequestBody ProductInfoAddRequest productInfoAddRequest, HttpServletRequest request) {
        if (productInfoAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ProductInfo productInfo = BeanUtil.copyProperties(productInfoAddRequest, ProductInfo.class);
        boolean save = productInfoService.save(productInfo);
        return ResultUtils.success(save);
    }




    /**
     * 删除产品信息
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @Operation(summary = "删除产品信息", description = "删除产品信息")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteProductInfo(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = productInfoService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新产品信息
     *
     * @param productInfoUpdateRequest
     * @param request
     * @return
     */
    @Operation(summary = "更新产品信息", description = "更新产品信息")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateProductInfo(@RequestBody ProductInfoUpdateRequest productInfoUpdateRequest, HttpServletRequest request) {
        if (productInfoUpdateRequest == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ProductInfo productInfo = new ProductInfo();
        BeanUtils.copyProperties(productInfoUpdateRequest, productInfo);
        boolean result = productInfoService.updateById(productInfo);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取产品信息
     *
     * @param id
     * @param request
     * @return
     */
    @Operation(summary = "根据 id 获取产品信息", description = "根据 id 获取产品信息")
    @GetMapping("/get")
    public BaseResponse<ProductInfoVO> getProductInfoById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ProductInfo productInfo = productInfoService.getById(id);
        ProductInfoVO productInfoVO = new ProductInfoVO();
        BeanUtils.copyProperties(productInfo, productInfoVO);
        return ResultUtils.success(productInfoVO);
    }

    /**
     * 获取产品信息列表
     *
     * @param productInfoQueryRequest
     * @param request
     * @return
     */
    @Operation(summary = "获取产品信息列表", description = "获取产品信息列表")
    @GetMapping("/list")
    public BaseResponse<List<ProductInfoVO>> listProductInfo(ProductInfoQueryRequest productInfoQueryRequest, HttpServletRequest request) {
        ProductInfo productInfoQuery = new ProductInfo();
        if (productInfoQueryRequest != null) {
            BeanUtils.copyProperties(productInfoQueryRequest, productInfoQuery);
        }
        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>(productInfoQuery);
        List<ProductInfo> productInfoList = productInfoService.list(queryWrapper);
        List<ProductInfoVO> productInfoVOList = productInfoList.stream().map(productInfo -> {
            ProductInfoVO productInfoVO = new ProductInfoVO();
            BeanUtils.copyProperties(productInfo, productInfoVO);
            return productInfoVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(productInfoVOList);
    }

    /**
     * 分页获取产品信息列表
     *
     * @param productInfoQueryRequest
     * @param request
     * @return
     */
    @Operation(summary = "分页获取产品信息列表", description = "分页获取产品信息列表")
    @GetMapping("/list/page")
    public BaseResponse<Page<ProductInfoVO>> listProductInfoByPage(ProductInfoQueryRequest productInfoQueryRequest, HttpServletRequest request) {
        return ResultUtils.success(productInfoService.listProductInfoByPage(productInfoQueryRequest,request));
    }

    // endregion

    /**
     * 下线产品
     * @param idRequest 产品id
     * @param request
     * @return
     */
    @PostMapping("/offline")
    public BaseResponse<Boolean> offlineInterface(@RequestBody IdRequest idRequest , HttpServletRequest request){
        if (idRequest == null ||  idRequest.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = productInfoService.offlineProduct(idRequest.getId(),request);
        return ResultUtils.success(res);
    }

    /**
     * 上线产品
     * @param idRequest 产品id
     * @param request
     * @return
     */
    @PostMapping("/online")
    public BaseResponse<Boolean> onlineInterface(@RequestBody IdRequest idRequest ,HttpServletRequest request){
        if (idRequest == null ||  idRequest.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
            boolean res = productInfoService.onlineProduct(idRequest.getId(),request);
        return ResultUtils.success(res);
    }




}
